package com.itcast.gameforum.controller;


import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itcast.gameforum.common.dto.MainEditDto;
import com.itcast.gameforum.common.lang.Result;
import com.itcast.gameforum.entity.Mainposts;
import com.itcast.gameforum.entity.User;
import com.itcast.gameforum.service.MainpostsService;
import com.itcast.gameforum.service.UserService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 647zyzlq
 * @since 2020-10-26
 */
@RestController
@RequestMapping("/mainposts/")
public class MainpostsController {

    @Autowired
    MainpostsService mainpostsService;

    @Autowired
    UserService userService;
    //返回所有主题
    @GetMapping("list")
    public Result list()
    {
        return new Result().succ(mainpostsService.list());
    }

    @GetMapping("{id}")
    public Result getOne(@PathVariable String id)
    {
        List<Mainposts> mainposts = mainpostsService.list(new QueryWrapper<Mainposts>().eq("zoneid", id));
        Assert.notNull(mainposts,"游戏专区不存在");
        return new Result().succ(mainposts);
    }

    @RequiresAuthentication
    @PostMapping("edit")
    public Result edit(@Valid @RequestBody MainEditDto mainEditDto)
    {
        User user = userService.getOne(new QueryWrapper<User>().eq("id", mainEditDto.getUserid()));
        Assert.notNull(user,"用户不能为空");
        Mainposts mainposts = new Mainposts();
        mainposts.setName(mainEditDto.getName());
        mainposts.setUserid(mainEditDto.getUserid());
        mainposts.setZoneid(mainEditDto.getZoneid());
        mainposts.setCreated(LocalDateTime.now());
        mainposts.setPostsnumber(1);

        mainpostsService.saveMainPost(mainposts);
        return new Result().succ(mainposts.getId());
    }

}
